/**
*    Copyright (C) 2008  Chase Kernan 
*    chase.kernan@gmail.com
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
**/

package com.chasekernan.hxnova.server;


#if neko
import neko.Sys;
import neko.io.File;
import neko.Lib;

/**
    Parses the command line arguments and calls the corresponding server 
    functions.
**/
class ServerEntry {
    
    private static var args : Array<String>;
    
    public static function run() {
        args = Sys.args();
        
        if (args.length == 0) {
            printHelpMessage();
            Sys.exit(0);
        }
        
        try {
            switch (args[0]) {
                
                case "help": printHelpMessage();
                
                case "create_game": createGame();
                
                case "run": runGame();
                
                default: 
                    Lib.print("Unkown argument: " + args[0] + "\n");
                    printHelpMessage();
                
            }
            
            Sys.exit(0);
        } catch (e : Dynamic) {
            Lib.print("An error occurred: \n" + e);
            Sys.exit(1);
        }
    }
    
    private static function printHelpMessage() {
        Lib.print(
        "hxnova takes the following arguments: \n" +
        "    help : prints this message \n" + 
        "    create_game path_to_game_file : creates a new game using the " +
                                              "given game file \n" +
        "    run game_name : runs the game with the given name \n"
        );
    }
    
    private static function createGame() {
        if (args.length != 2) throw "create_game takes 1 argument.";
        
        var xml = Xml.parse(File.getContent(args[1])).firstChild();
        Game.createNewGameFromXml(xml);
    }
    
    private static function runGame() {
        if (args.length != 2) throw "run_game takes 1 argument";
        
        Server.runTurn(args[1]);
    }
}
#end
